import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/extension/extension_string.dart';
import 'package:xindong/manager/xindong_file_singleton.dart';
import 'package:xindong/models/xindong_city_class.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/dialog/xindong_selector3_sheet.dart';

import 'xindong_selector2_sheet.dart';

class XinDongCitiesSheet {
  /// 弹出默认地址
  /// 只返回
  static void show({String? selectText, required Function(String text) onDone}) async {
    List<XinDongSelectorClass> dataList = await defaultAddress();
    List<XinDongSelectorClass>? selectedList;
    if (!TextUtil.isEmpty(selectText)) {
      List<String> values = selectText!.stringToList(pattern: " ");
      print(values);
      for (XinDongSelectorClass item in dataList) {
        if (item.value == values.first) {

        }
      }
      for (int i = 0; i < dataList.length; i++) {
      }
    }

    XinDongSelectorSheet.show(
      title: '选择城市',
      name: "当前选择",
      dataList: dataList,
      selectedList: selectedList,
      onDone: (List<XinDongSelectorClass> selectedList) {
        onDone("${selectedList[1].value.replaceFirst("市", "")} ${selectedList[2].value.replaceFirst("区", "")}");
      },
    );
  }

  static void showCity({String? selectText, required Function(String text) onDone}) async {
    List<XinDongSelectorClass> dataList = await defaultAddress();
    List<XinDongSelectorClass>? selectedList;
    if (!TextUtil.isEmpty(selectText)) {
      List<String> values = selectText!.stringToList(pattern: " ");
      print(values);
      for (XinDongSelectorClass item in dataList) {
        if (item.value == values.first) {

        }
      }
      for (int i = 0; i < dataList.length; i++) {
      }
    }

    XinDongSelectorTwoSheet.show(
      title: '选择城市',
      name: "当前选择",
      dataList: dataList,
      selectedList: selectedList,
      onDone: (List<XinDongSelectorClass> selectedList) {
        onDone(selectedList[1].value);
      },
    );
  }

  // 默认
  static Future<List<XinDongSelectorClass>> defaultAddress() async {
    // 全局数据
    showLoading();
    if (XinDongFileManager.instance.cities.isEmpty) {
      List list = await XinDongFileManager.instance.loadJsonFile(AssetsRes.CITY);
      if (list.isNotEmpty) {
        XinDongFileManager.instance.cities = list.map((e) => XinDongCityClass.fromJson(e)).toList();
      }
    }

    // 内嵌方法
    XinDongSelectorClass convertToSelectorClass(XinDongCityClass model) {
      // 构造模型
      XinDongSelectorClass selectorClass = XinDongSelectorClass();
      selectorClass.key = model.name;
      selectorClass.value = model.name;

      // 下级列表
      List list = model.sub ?? [];
      // 递归转换
      selectorClass.nextList = list.map((e) => convertToSelectorClass(e)).toList();
      return selectorClass;
    }

    // 模型转换
    List<XinDongSelectorClass> dataList =
        XinDongFileManager.instance.cities.map((e) => convertToSelectorClass(e)).toList();

    hideLoading();
    return dataList;
  }
}
